package socket_net_code.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Demo1_Receive {
    public Demo1_Receive() throws SocketException {
    }

    /*
        接收Receive
            创建DatagramSocket, 指定端口号
            创建DatagramPacket, 指定数组, 长度
            使用DatagramSocket接收DatagramPacket
            关闭DatagramSocket
            从DatagramPacket中获取数据
         */
    public static void main(String[] args) throws IOException {
        //创建Socket相当于码头
        DatagramSocket socket = new DatagramSocket(6666);
        //创建Packet相当于创建集装箱
        DatagramPacket packet = new DatagramPacket(new byte[1024],1024);
        //接货，接收数据
        socket.receive(packet);
        //获取数据
        byte[] arr = packet.getData();
        //获取有效的字节个数
        int len = packet.getLength();
        System.out.println(new String(arr, 0, len));

        socket.close();
    }


}
